#!/bin/bash
#2017/09/24

#麒麟64位防火墙服务： firewall
##永久生效的意思： --permanent
##添加服务： firewall-cmd  --permanent --add-service=redis
##重启服务：firewall-cmd --complete-reload
##添加端口： firewall-cmd --permanent --add-port=6379/tcp
##删除端口： firewall-cmd --remove-port=6379/tcp

fport=port.txt
port_ret="`awk -F"=" '/port=/{print $2}' ${fport}`"



	if [ ! -f ${fport} ]; then
		echo "Error:not find ${fport} ...stoping"
		exit
	fi

function f_addfiewall(){
	i=1
	for p in ${port_ret}
	do
#	echo "port${i} = ${p}";
	port_p="`echo ${p} | awk -F":" '{print $1}'`";
	port_s="`echo ${p} | awk -F":" '{print $2}'`";
	echo ${port_p}/${port_s};
	firewall-cmd --permanent --add-port=${port_p}/${port_s}
	let i++;
	done
	firewall-cmd --complete-reload
}

function f_delfiewall(){
	i=1
	for p in ${port_ret}
	do
#	echo "port${i} = ${p}";
	port_p="`echo ${p} | awk -F":" '{print $1}'`";
	port_s="`echo ${p} | awk -F":" '{print $2}'`";
	echo ${port_p}/${port_s};
	firewall-cmd --permanent --del-port=${port_p}/${port_s}
	let i++;
	done
	firewall-cmd --complete-reload
}
function f_rule(){
	firewall-cmd --list-all
}

source ./../os/os.sh --os-type
#回调os_ret=-1其他系统，1：麒麟桌面版64，2：麒麟服务器版，3：redhat6.5,4：ubuntu
case "$1" in
	addfirewall)
		if [ "$(command -v systemctl)" ] && [ "$(command -v firewall-cmd)" ]; then
			echo "os is kyelin 64"
			f_addfiewall
		else 
			echo "This is is no support"
		fi
		#echo "${os_ret}"
	;;
	delfiewall)
		if [ "$(command -v systemctl)" ] && [ "$(command -v firewall-cmd)" ]; then
			echo "os is kyelin 64"
			f_delfiewall
		else 
			echo "This is is no support"
		fi
	;;
	fwrule)
		if [ "$(command -v systemctl)" ] && [ "$(command -v firewall-cmd)" ]; then
			echo "os is use firewalld"
			fw_stat="`firewall-cmd --stat`"
			if [ "${fw_stat}" = "not running" ]; then
				echo "firewall is not running,so exit"
			else
				f_rule
			fi
		elif [ "$(command -v chkconfig)" ] && [ "$(command -v iptables)" ]; then
			service iptables status			
		else 
			echo "This is is no support"
		fi
	;;
	*)
		echo "Please use addfirewall or delfiewall as first argument"
	;;
esac



